/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/10 23:26
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
(function () {

    "use strict";

    var api = {
        getBbsDetail: '/bbs/detail/content',
        getBbsListsUrl: '/bbs/lists/getLists',
        removeItemUrl: '/bbs/lists/remove',
        getCommentsListsUrl: '/bbs/detail/getComments',
        deleteComments: '/bbs/detail/deleComment',
        deleteReplyUrl: '/bbs/detail/deleteReply'
    };
    var all = ['all', 'text', 'mixed'];

    sm.controller('admin-bbs-detail-controller', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {

        $scope.lists = [];
        $scope.page = {
            page: 1,
            type: "all"
        };
        $scope.nextPage = false;
        $scope.attachment = {};
        $scope.comments = [];
        $scope.cpage = {
            page: 1,
            bbsId: GetQueryString('bbsId')
        };

        $scope.commentsNextPage = false;
        $scope.detail = {};
        $scope.attachment = [];

        $http.get(api.getBbsDetail, {
            params: {
                bbsId: GetQueryString('bbsId')
            }
        }).success(function (res) {
            if (res.state) {
                $scope.detail = res.data;
                $scope.attachment = res.data.attachment;
            }
        });



        $scope.showImage = function () {
            $('#imageModal').modal();
        };

        $scope.loadMoreComments = function () {
            if ($scope.commentsNextPage) {
                $scope.cpage.page += 1;
            }
            getCommentsLists();
        };

        $scope.removeItem = function (bbsId, key) {
            Modal.alert({
                'title': '您确定要删除此贴吗?',
                'content': '删除之后将不能恢复,请谨慎操作.',
                'buttons': [{
                    'name': '取消'
                }, {
                    'name': '确认删除',
                    'event': function () {
                        removeBBS(bbsId, key)
                    }
                }]
            });
        };

        $scope.trushComment = function (commentId, key) {
            if (!commentId) {
                return false;
            }

            Modal.alert({
                'title': '您确定删除该评论吗?',
                'content': '删除的评论将连同回复一起删除,请谨慎操作.',
                'buttons': [
                    {
                        'name': '取消'
                    }, {
                        'name': '确定删除',
                        'event': function () {
                            deleteComment(commentId, key);
                        }
                    }
                ]
            });
        };

        // 删除评论
        function deleteComment(cId, key) {
            $http.get(api.deleteComments + '?commentId=' + cId).success(function (res) {
                if (res.state) {
                    $scope.comments.splice(key, 1);
                } else {
                    var error = error ? error : '删除失败';
                    Modal.error(error);
                }
            });
        }

        $scope.trushReply = function (replyId, key) {
            if (!replyId) {
                return false;
            }

            Modal.alert({
                'title': '您确定删除该回复吗?',
                'content': '删除后将不能恢复,请谨慎操作.',
                'buttons': [
                    {
                        'name': '取消'
                    }, {
                        'name': '确定删除',
                        'event': function () {
                            deleteReply(replyId, key);
                        }
                    }
                ]
            });
        };

        function deleteReply(replyId, key) {
            $http.get(api.deleteReplyUrl + '?replyId=' + replyId).success(function (res) {
                if (res.state) {
                    $scope.comments[key].reply = {};
                } else {
                    var error = error ? error : '删除失败';
                    Modal.error(error);
                }
            });
        }

        // 获取评论列表
        function getCommentsLists()
        {
            $http.get(api.getCommentsListsUrl, {
                params: $scope.cpage
            }).success(function (res) {
                if (res.state) {
                    $scope.commentsNextPage = res.data.nextPage;
                    res.data.data && appendCommLists(res.data.data);
                }
            });
        }
        getCommentsLists();

        // 删除
        function removeBBS(bbsId, key) {
            $http.get(api.removeItemUrl, {
                params: {
                    bbsId: bbsId
                }
            }).success(function (res) {
                if (res.state) {
                    $scope.lists.splice(key, 1);
                } else {
                    var error = res.error ? res.error : '删除失败';
                    Modal.error(error);
                }
            });
        }

        function appendCommLists(lists) {
            if (lists.length == 0) {
                return false;
            }

            angular.forEach(lists, function (v) {
                $scope.comments.push(v);
            });
        }
    }]);

})();